import { ProductList } from '../../pages/product/list-model.js';
var productList = new ProductList();
const app = getApp();

Component({
  properties: {
    productData: {
      type: Object,
      value: '',
    },
    page:{
      type:Number,
      value:1,
      hiddenNomore: true,
      hiddenLoading: true,
      hiddenNullData: true,
      onLoading: false,
      nomore: false,
      shop_id:'',
      id:'',
      code:'',
      showShare:false,
      action:''
    }
  },
  data: {
  },

  ready: function () {
  },
  methods: {
    /*跳转到商品详情*/
    onProductsItemTap: function (e) {
      var id = e.currentTarget.dataset.id;
      var uid = wx.getStorageSync('uid');
      wx.navigateTo({
        url: 'edit?id=' + id
      })
    },
      /**
     * 展示操作弹框
     */
    sheet: function (e) {
      var id  = e.currentTarget.dataset.id;
      var index = e.currentTarget.dataset.index;
      this.data.code = e.currentTarget.dataset.code;
      this.data.shop_id = e.currentTarget.dataset.shop_id;
      if(!id){
        wx.showModal({
          title: '提示',
          content: '该数据有误',
        })
        return false;
      }
      this.data.id  = id;
      this.data.index = index;
      this.setData({
        showShare: true
      })
    },
    /**
    * 隐藏操作弹框
    */
    hiddenSheet: function () {
      this.setData({
        showShare: false
      })
    },
    /**
  * 删除
  */
  del: function () {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您确定要删除该商品吗',
      success(res) {
        if (res.confirm) {
          var args ={
            product_id : that.data.id,
            action:'delete'
          }
          productList.product_status(args, (res) => {
            that.hiddenSheet();
            if(res.status == 1 ){
              that.data.productData.splice(that.data.index, 1);
              var data = that.data.productData;
              that.setData({
                productData : data
              })
              wx.showToast({
                title: '删除成功',
                  icon: 'success',
                  duration: 1000,
              })
            }else{
              wx.showModal({
                title: '提示',
                content: res.msg ? res.msg : '删除失败',
              })
            }
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
    //上架
    up:function(){
      var that = this;
      wx.showModal({
        title: '提示',
        content: '您确定要上架该商品吗',
        success(res) {
          console.log(res)
          if (res.confirm) {
            var args ={
              product_id : that.data.id,
              status:3,
              action:'status'
            }
            productList.product_status(args, (res) => {
              that.hiddenSheet();
              if(res.status == 1 ){
                that.data.productData[that.data.index].status = 3;
                var data = that.data.productData;
                that.setData({
                  productData : data
                })
                // var item = 'productData['+that.data.index+'].status';
                // that.setData({
                //   [item] : 3
                // })
                wx.showToast({
                  title: '上架成功',
                  icon: 'success',
                  duration: 1000,
                })
              }else{
                wx.showModal({
                  title: '提示',
                  content: res.msg ? res.msg : '上架失败',
                })
              }
            });
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },
    /**
    * 下架
    */
    down: function () {
      var that = this;
      wx.showModal({
        title: '提示',
        content: '您确定要下架该商品吗',
        success(res) {
          if (res.confirm) {
            var args ={
              product_id : that.data.id,
              status:2,
              action:'status'
            }
            productList.product_status(args, (res) => {
              that.hiddenSheet();
              if(res.status == 1 ){
                that.data.productData[that.data.index].status=2;
                var data=that.data.productData;
                that.setData({
                  productData:data
                })
                wx.showToast({
                  title: '下架成功',
                  icon: 'success',
                  duration: 1000,
                })
              }else{
                wx.showModal({
                  title: '提示',
                  content: res.msg ? res.msg : '下架失败',
                })
              }
            });
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },
  },
})